package springapp.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class CreerInfractionValidator implements Validator {
   
    /** Logger for this class and subclasses */
    protected final Log logger                 = LogFactory.getLog(getClass());

    public boolean supports(Class clazz) {
        return InfractionCommand.class.equals(clazz);
    }

    public void validate(Object obj, Errors errors) {
    	InfractionCommand nouvelleInfraction = (InfractionCommand) obj;
    	
    	ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "Infraction.DescriptionInvalide", "Required field");
		
    	ValidationUtils.rejectIfEmptyOrWhitespace(errors, "niveau", "Infraction.NiveauInvalide", "Required field");

		if (!errors.hasFieldErrors("niveau")) {
			if (nouvelleInfraction.getNiveau()<1 || nouvelleInfraction.getNiveau()>10){
				errors.rejectValue("niveau", "Infraction.NiveauInvalide", "Invalid infraction level");
			}
		}		
    }
}